package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysMsgNum;
import com.ruoyi.system.service.ISysDictDataService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMsgMapper;
import com.ruoyi.system.domain.SysMsg;
import com.ruoyi.system.service.ISysMsgService;
import com.ruoyi.common.core.text.Convert;

/**
 * 消息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@Service
public class SysMsgServiceImpl implements ISysMsgService 
{
    @Autowired
    private SysMsgMapper sysMsgMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询消息
     * 
     * @param id 消息主键
     * @return 消息
     */
    @Override
    public SysMsg selectSysMsgById(Long id)
    {
        return sysMsgMapper.selectSysMsgById(id);
    }

    /**
     * 查询消息列表
     * 
     * @param sysMsg 消息
     * @return 消息
     */
    @Override
    public List<SysMsg> selectSysMsgList(SysMsg sysMsg)
    {
        return sysMsgMapper.selectSysMsgList(sysMsg);
    }

    /**
     * 新增消息
     * 
     * @param sysMsg 消息
     * @return 结果
     */
    @Override
    public int insertSysMsg(SysMsg sysMsg)
    {
        sysMsg.setCreateTime(DateUtils.getNowDate());
        return sysMsgMapper.insertSysMsg(sysMsg);
    }

    /**
     * 修改消息
     * 
     * @param sysMsg 消息
     * @return 结果
     */
    @Override
    public int updateSysMsg(SysMsg sysMsg)
    {
        sysMsg.setUpdateTime(DateUtils.getNowDate());
        return sysMsgMapper.updateSysMsg(sysMsg);
    }

    /**
     * 批量删除消息
     * 
     * @param ids 需要删除的消息主键
     * @return 结果
     */
    @Override
    public int deleteSysMsgByIds(String ids)
    {
        return sysMsgMapper.deleteSysMsgByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除消息信息
     * 
     * @param id 消息主键
     * @return 结果
     */
    @Override
    public int deleteSysMsgById(Long id)
    {
        return sysMsgMapper.deleteSysMsgById(id);
    }

    @Override
    public int readFlagByIds(String ids) {
        return sysMsgMapper.readFlagByIds(Convert.toStrArray(ids));
    }

    @Override
    public int readFlagByReceiverId(Long receiverId) {
        return sysMsgMapper.readFlagByReceiverId(receiverId);
    }

    @Override
    public List<SysMsgNum> selectSysMsgNum() {
        List<SysMsgNum> retSysMsgNums = new ArrayList<>();
        List<SysMsgNum> sysMsgNums = sysMsgMapper.selectSysMsgNum(ShiroUtils.getSysUser().getUserId());
        SysDictData dictDataParam = new SysDictData();
        dictDataParam.setDictType("sys_msg_type");
        List<SysDictData> sysDictDatas = sysDictDataService.selectDictDataList(dictDataParam);
        for(SysDictData sysDictData: sysDictDatas){
            SysMsgNum sysMsgNum = new SysMsgNum();
            sysMsgNum.setTypeName(sysDictData.getDictLabel());
            sysMsgNum.setIcon(sysDictData.getCssClass());
            sysMsgNum.setType(sysDictData.getDictValue());
            Optional<SysMsgNum> sysMsgNumOptional =  sysMsgNums.stream().filter(it->it.getType().equals(sysDictData.getDictValue())).findFirst();
            if(sysMsgNumOptional.isPresent()){
                sysMsgNum.setNum(sysMsgNumOptional.get().getNum());
            }else{
                sysMsgNum.setNum(0);
            }
            retSysMsgNums.add(sysMsgNum);
        }
        return retSysMsgNums;
    }
}
